package api;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import appmodel.AppUserLabel;
import appmodel.JsonArrayResult;
import appmodel.JsonModel;
import model.User;
import net.sf.json.JSONObject;
import service.CommentService;
import service.IncomeService;
import service.UserService;

@Controller
@RequestMapping({ "/user" })
public class LoginApiController {

	@Autowired
	private UserService userService;

	@Autowired
	private IncomeService incomeService;

	@Autowired
	private CommentService commentService;

	/**
	 * 登录接口
	 */
	@RequestMapping(value = { "/login" }, method = RequestMethod.POST)
	@ResponseBody
	public JSONObject login(HttpServletRequest request, JsonModel jsonModel,
			@RequestParam(value = "username", required = false) String account,
			@RequestParam(value = "pwd", required = false) String pwd) {
		User user = new User();
		user.setAccount(account);
		user.setPassword(pwd);

		User result = this.userService.applogin(user);

		if (null != result) {
			Long incomeAll = incomeService.getIncomeAll(result.getUserId());
			if(incomeAll == null) {
				incomeAll = 0l;
			}
			Integer averageScore = commentService.getCommentAverageScoreByUser(result.getUserId());
			if(null == averageScore) {
				averageScore = 0;
			}

			result.setIncome(incomeAll);
			result.setScore(averageScore);
			
			jsonModel.setSuccess(true);
			jsonModel.setMsg("查询成功");
			jsonModel.setjData(result);
			jsonModel.setToken("");
		} else {
			jsonModel.setSuccess(false);
			jsonModel.setMsg("账号或密码错误");
			jsonModel.setjData("");
			jsonModel.setToken("");
		}

		return JSONObject.fromObject(jsonModel);
	}

	/**
	 * 获取用户信息
	 */
	@RequestMapping(value = { "/userinfo" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public JSONObject userinfo(HttpServletRequest request, JsonModel jsonModel,
			@RequestParam(value = "userId", required = false) long userId) {

		User userNew = this.userService.getUserInfo(userId);

		if (null != userNew) {
			Long incomeAll = incomeService.getIncomeAll(userId);
			if(incomeAll == null) {
				incomeAll = 0l;
			}
			Integer averageScore = commentService.getCommentAverageScoreByUser(userId);
			if(null == averageScore) {
				averageScore = 0;
			}

			userNew.setIncome(incomeAll);
			userNew.setScore(averageScore);

			jsonModel.setSuccess(true);
			jsonModel.setMsg("查询成功");
			jsonModel.setjData(userNew);
			jsonModel.setToken("");
		} else {
			jsonModel.setSuccess(false);
			jsonModel.setMsg("查询失败");
			jsonModel.setjData("");
			jsonModel.setToken("");
		}
		return JSONObject.fromObject(jsonModel);
	}

	/**
	 * 修改地址信息接口
	 */
	@RequestMapping(value = { "/modifyAddr" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public JSONObject modifyAddrInfo(HttpServletRequest request, JsonModel jsonModel,
			@RequestParam(value = "user_name", required = false) String user_name,
			@RequestParam(value = "phone", required = false) String phone,
			@RequestParam(value = "user_id", required = false) long user_id,
			@RequestParam(value = "adress", required = false) String adress) {
		User user = new User();
		user.setUserName(user_name);
		user.setPhone(phone);
		user.setAdress(adress);
		user.setUserId(user_id);

		this.userService.modifyAddrInfo(user);

		jsonModel.setSuccess(true);
		jsonModel.setMsg("查询成功");
		jsonModel.setjData("");
		jsonModel.setToken("");
		return JSONObject.fromObject(jsonModel);
	}
	

	/**
	 * 修改卡号信息
	 */
	@RequestMapping(value = { "/modifyCardNo" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public JSONObject modifyCardNo(HttpServletRequest request, JsonModel jsonModel,
			@RequestParam(value = "card_user_name", required = false) String card_user_name,
			@RequestParam(value = "card_name", required = false) String card_name,
			@RequestParam(value = "user_id", required = false) long user_id,
			@RequestParam(value = "card_no", required = false) String card_no) {
		User user = new User();
		user.setCardUserName(card_user_name);
		user.setCardName(card_name);
		user.setCardNo(card_no);
		user.setUserId(user_id);

		this.userService.modifyCardNo(user);

		jsonModel.setSuccess(true);
		jsonModel.setMsg("查询成功");
		jsonModel.setjData("");
		jsonModel.setToken("");
		return JSONObject.fromObject(jsonModel);
	}
	
	
	/**
	 * 个人中心-获取我的信息
	 */
	@RequestMapping(value = { "/getmylabels" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public JSONObject getMyLabels(HttpServletRequest request, JsonModel jsonModel,
			@RequestParam(value = "userId", required = false) long user_id) {

		List<AppUserLabel> list = this.userService.getMyLabels(user_id);
		JsonArrayResult arrayResult = new JsonArrayResult();
		arrayResult.setJsonArrayResult(list);
		if(null != list) {
			jsonModel.setSuccess(true);
			jsonModel.setMsg("查询成功");
			jsonModel.setjData(arrayResult);
			jsonModel.setToken("");
		}else {
			jsonModel.setSuccess(true);
			jsonModel.setMsg("查询失败");
			jsonModel.setjData("");
			jsonModel.setToken("");
		}
		
		return JSONObject.fromObject(jsonModel);
	}

}